package com.wang.myapplication.base;

import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xhwang on 2018/4/27.
 */

public abstract class RecyclerBaseAdapter<T> extends RecyclerView.Adapter<RecyclerBaseAdapter.MyBaseViewHolder> {
    private List<T> mDataLists = new ArrayList<>();//为什么不是private List<T> mDataLists; 为了初始化时,这个是不能为null的,下面getItem getCount都需要。所以设置个默认0
    //private Context mContext;//是否必须呢
    private int layoutId;

    private View.OnClickListener mOnClickListener;
    private View.OnLongClickListener mOnLongClickListener;
    private MyBaseViewHolder myBaseViewHolder;


    public RecyclerBaseAdapter(List<T> pDataLists) {
        mDataLists = pDataLists;
    }

    public RecyclerBaseAdapter(int pLayoutId) {
        layoutId = pLayoutId;
    }

    public RecyclerBaseAdapter(List<T> pDataLists, int pLayoutId) {
        mDataLists = pDataLists;
        layoutId = pLayoutId;
    }

    public void setOnClickListener(View.OnClickListener pOnClickListener) {
        mOnClickListener = pOnClickListener;
    }

    public void setOnLongClickListener(View.OnLongClickListener pOnLongClickListener) {
        mOnLongClickListener = pOnLongClickListener;
    }

    @Override
    public MyBaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
        myBaseViewHolder = new MyBaseViewHolder(itemView);
        if (mOnClickListener != null)
            myBaseViewHolder.itemView.setOnClickListener(mOnClickListener);
        if (mOnLongClickListener != null)
            myBaseViewHolder.itemView.setOnLongClickListener(mOnLongClickListener);
        return myBaseViewHolder;
    }

    @Override
    public void onBindViewHolder(MyBaseViewHolder holder, int position) {
        handItemView(holder, mDataLists.get(position), position);
        holder.setTag(mDataLists.get(position));
        holder.setPosition(position);
    }


    public List<T> getDataLists() {
        return mDataLists;
    }


    public void addDataLists(List<T> pDataLists) {
        if (pDataLists != null) {
            mDataLists.addAll(pDataLists);
            notifyDataSetChanged();
        }
    }

    public void replaceDataList(List<T> dataList) {
        if (dataList != null) {
            mDataLists.clear();
            mDataLists.addAll(dataList);
        } else {
            mDataLists.clear();
        }
        notifyDataSetChanged();
    }

    //添加一个元素
    public void add(T data) {
        if (mDataLists == null) {
            mDataLists = new ArrayList<>();
        }
        mDataLists.add(data);
        notifyDataSetChanged();
    }

    //往特定位置，添加一个元素
    public void add(int position, T data) {
        if (mDataLists == null) {
            mDataLists = new ArrayList<>();
        }
        mDataLists.add(position, data);
        notifyDataSetChanged();
    }


    public boolean removeData(T item) {
        boolean result = false;
        if (item != null) {
            mDataLists.remove(item);
            notifyDataSetChanged();
            result = true;
        }
        return result;
    }

    public boolean removeData(int index) {
        boolean result = false;
        if (index >= 0 && index < mDataLists.size()) {
            mDataLists.remove(index);
            notifyDataSetChanged();
            result = true;
        }
        return result;
    }

    public void clear() {
        if (mDataLists != null) {
            mDataLists.clear();
        }
        notifyDataSetChanged();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mDataLists.size();
    }


    /**
     * 子类必须重写的方法，通过这个方法初始化holder中的View即可
     */
    public abstract void handItemView(MyBaseViewHolder holder, T t, int pPosition);


    protected static class MyBaseViewHolder extends RecyclerView.ViewHolder {
        private SparseArray<View> views;
        private int position;

        public SparseArray<View> getViews() {
            return views;
        }

        public void setViews(SparseArray<View> pViews) {
            views = pViews;

        }

        public void setPosition(int pPosition) {
            position = pPosition;
        }


        public void setTag(Object pTag) {
            this.itemView.setTag(pTag);
        }

        public MyBaseViewHolder(View itemView) {
            super(itemView);
            this.views = new SparseArray<View>();
        }

        @SuppressWarnings("unchecked")
        public <T extends View> T findViewById(int id) {
            View view = views.get(id); // 同id的控件可重复使用，无需再次findViewById
            if (view == null) {
                view = this.itemView.findViewById(id);
                views.append(id, view); // 将此id的控件添加进sparseArray
            }
            return (T) view;
        }

    }


}
